/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.cggveritas.crewintranet.jms.dao.jpacontroller;

import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.IllegalOrphanException;
import fr.cggveritas.crewintranet.jms.dao.jpacontroller.exceptions.NonexistentEntityException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import fr.cggveritas.crewintranet.jms.entities.CvMarque;
import fr.cggveritas.crewintranet.jms.entities.CvModel;
import fr.cggveritas.crewintranet.jms.entities.CvTypemodel;
import java.util.ArrayList;
import java.util.List;
import fr.cggveritas.crewintranet.jms.entities.CvVehicule;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

/**
 *
 * @author Alassane
 */
public class CvModelJpaController extends DaoImp implements Serializable {

    public CvModelJpaController() {
        super();
    }

    public void create(CvModel cvModel) {
        if (cvModel.getCvTypemodelList() == null) {
            cvModel.setCvTypemodelList(new ArrayList<CvTypemodel>());
        }
        if (cvModel.getCvVehiculeList() == null) {
            cvModel.setCvVehiculeList(new ArrayList<CvVehicule>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvMarque cvMarqueIdmarque = cvModel.getCvMarqueIdmarque();
            if (cvMarqueIdmarque != null) {
                cvMarqueIdmarque = em.getReference(cvMarqueIdmarque.getClass(), cvMarqueIdmarque.getIdmarque());
                cvModel.setCvMarqueIdmarque(cvMarqueIdmarque);
            }
            List<CvTypemodel> attachedCvTypemodelList = new ArrayList<CvTypemodel>();
            for (CvTypemodel cvTypemodelListCvTypemodelToAttach : cvModel.getCvTypemodelList()) {
                cvTypemodelListCvTypemodelToAttach = em.getReference(cvTypemodelListCvTypemodelToAttach.getClass(), cvTypemodelListCvTypemodelToAttach.getIdcvTypemodel());
                attachedCvTypemodelList.add(cvTypemodelListCvTypemodelToAttach);
            }
            cvModel.setCvTypemodelList(attachedCvTypemodelList);
            List<CvVehicule> attachedCvVehiculeList = new ArrayList<CvVehicule>();
            for (CvVehicule cvVehiculeListCvVehiculeToAttach : cvModel.getCvVehiculeList()) {
                cvVehiculeListCvVehiculeToAttach = em.getReference(cvVehiculeListCvVehiculeToAttach.getClass(), cvVehiculeListCvVehiculeToAttach.getIdvehicule());
                attachedCvVehiculeList.add(cvVehiculeListCvVehiculeToAttach);
            }
            cvModel.setCvVehiculeList(attachedCvVehiculeList);
            em.persist(cvModel);
            if (cvMarqueIdmarque != null) {
                cvMarqueIdmarque.getCvModelList().add(cvModel);
                cvMarqueIdmarque = em.merge(cvMarqueIdmarque);
            }
            for (CvTypemodel cvTypemodelListCvTypemodel : cvModel.getCvTypemodelList()) {
                CvModel oldCvModelIdmodelOfCvTypemodelListCvTypemodel = cvTypemodelListCvTypemodel.getCvModelIdmodel();
                cvTypemodelListCvTypemodel.setCvModelIdmodel(cvModel);
                cvTypemodelListCvTypemodel = em.merge(cvTypemodelListCvTypemodel);
                if (oldCvModelIdmodelOfCvTypemodelListCvTypemodel != null) {
                    oldCvModelIdmodelOfCvTypemodelListCvTypemodel.getCvTypemodelList().remove(cvTypemodelListCvTypemodel);
                    oldCvModelIdmodelOfCvTypemodelListCvTypemodel = em.merge(oldCvModelIdmodelOfCvTypemodelListCvTypemodel);
                }
            }
            for (CvVehicule cvVehiculeListCvVehicule : cvModel.getCvVehiculeList()) {
                CvModel oldCvModelIdmodelOfCvVehiculeListCvVehicule = cvVehiculeListCvVehicule.getCvModelIdmodel();
                cvVehiculeListCvVehicule.setCvModelIdmodel(cvModel);
                cvVehiculeListCvVehicule = em.merge(cvVehiculeListCvVehicule);
                if (oldCvModelIdmodelOfCvVehiculeListCvVehicule != null) {
                    oldCvModelIdmodelOfCvVehiculeListCvVehicule.getCvVehiculeList().remove(cvVehiculeListCvVehicule);
                    oldCvModelIdmodelOfCvVehiculeListCvVehicule = em.merge(oldCvModelIdmodelOfCvVehiculeListCvVehicule);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(CvModel cvModel) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvModel persistentCvModel = em.find(CvModel.class, cvModel.getIdmodel());
            CvMarque cvMarqueIdmarqueOld = persistentCvModel.getCvMarqueIdmarque();
            CvMarque cvMarqueIdmarqueNew = cvModel.getCvMarqueIdmarque();
            List<CvTypemodel> cvTypemodelListOld = persistentCvModel.getCvTypemodelList();
            List<CvTypemodel> cvTypemodelListNew = cvModel.getCvTypemodelList();
            List<CvVehicule> cvVehiculeListOld = persistentCvModel.getCvVehiculeList();
            List<CvVehicule> cvVehiculeListNew = cvModel.getCvVehiculeList();
            List<String> illegalOrphanMessages = null;
            for (CvTypemodel cvTypemodelListOldCvTypemodel : cvTypemodelListOld) {
                if (!cvTypemodelListNew.contains(cvTypemodelListOldCvTypemodel)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain CvTypemodel " + cvTypemodelListOldCvTypemodel + " since its cvModelIdmodel field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (cvMarqueIdmarqueNew != null) {
                cvMarqueIdmarqueNew = em.getReference(cvMarqueIdmarqueNew.getClass(), cvMarqueIdmarqueNew.getIdmarque());
                cvModel.setCvMarqueIdmarque(cvMarqueIdmarqueNew);
            }
            List<CvTypemodel> attachedCvTypemodelListNew = new ArrayList<CvTypemodel>();
            for (CvTypemodel cvTypemodelListNewCvTypemodelToAttach : cvTypemodelListNew) {
                cvTypemodelListNewCvTypemodelToAttach = em.getReference(cvTypemodelListNewCvTypemodelToAttach.getClass(), cvTypemodelListNewCvTypemodelToAttach.getIdcvTypemodel());
                attachedCvTypemodelListNew.add(cvTypemodelListNewCvTypemodelToAttach);
            }
            cvTypemodelListNew = attachedCvTypemodelListNew;
            cvModel.setCvTypemodelList(cvTypemodelListNew);
            List<CvVehicule> attachedCvVehiculeListNew = new ArrayList<CvVehicule>();
            for (CvVehicule cvVehiculeListNewCvVehiculeToAttach : cvVehiculeListNew) {
                cvVehiculeListNewCvVehiculeToAttach = em.getReference(cvVehiculeListNewCvVehiculeToAttach.getClass(), cvVehiculeListNewCvVehiculeToAttach.getIdvehicule());
                attachedCvVehiculeListNew.add(cvVehiculeListNewCvVehiculeToAttach);
            }
            cvVehiculeListNew = attachedCvVehiculeListNew;
            cvModel.setCvVehiculeList(cvVehiculeListNew);
            cvModel = em.merge(cvModel);
            if (cvMarqueIdmarqueOld != null && !cvMarqueIdmarqueOld.equals(cvMarqueIdmarqueNew)) {
                cvMarqueIdmarqueOld.getCvModelList().remove(cvModel);
                cvMarqueIdmarqueOld = em.merge(cvMarqueIdmarqueOld);
            }
            if (cvMarqueIdmarqueNew != null && !cvMarqueIdmarqueNew.equals(cvMarqueIdmarqueOld)) {
                cvMarqueIdmarqueNew.getCvModelList().add(cvModel);
                cvMarqueIdmarqueNew = em.merge(cvMarqueIdmarqueNew);
            }
            for (CvTypemodel cvTypemodelListNewCvTypemodel : cvTypemodelListNew) {
                if (!cvTypemodelListOld.contains(cvTypemodelListNewCvTypemodel)) {
                    CvModel oldCvModelIdmodelOfCvTypemodelListNewCvTypemodel = cvTypemodelListNewCvTypemodel.getCvModelIdmodel();
                    cvTypemodelListNewCvTypemodel.setCvModelIdmodel(cvModel);
                    cvTypemodelListNewCvTypemodel = em.merge(cvTypemodelListNewCvTypemodel);
                    if (oldCvModelIdmodelOfCvTypemodelListNewCvTypemodel != null && !oldCvModelIdmodelOfCvTypemodelListNewCvTypemodel.equals(cvModel)) {
                        oldCvModelIdmodelOfCvTypemodelListNewCvTypemodel.getCvTypemodelList().remove(cvTypemodelListNewCvTypemodel);
                        oldCvModelIdmodelOfCvTypemodelListNewCvTypemodel = em.merge(oldCvModelIdmodelOfCvTypemodelListNewCvTypemodel);
                    }
                }
            }
            for (CvVehicule cvVehiculeListOldCvVehicule : cvVehiculeListOld) {
                if (!cvVehiculeListNew.contains(cvVehiculeListOldCvVehicule)) {
                    cvVehiculeListOldCvVehicule.setCvModelIdmodel(null);
                    cvVehiculeListOldCvVehicule = em.merge(cvVehiculeListOldCvVehicule);
                }
            }
            for (CvVehicule cvVehiculeListNewCvVehicule : cvVehiculeListNew) {
                if (!cvVehiculeListOld.contains(cvVehiculeListNewCvVehicule)) {
                    CvModel oldCvModelIdmodelOfCvVehiculeListNewCvVehicule = cvVehiculeListNewCvVehicule.getCvModelIdmodel();
                    cvVehiculeListNewCvVehicule.setCvModelIdmodel(cvModel);
                    cvVehiculeListNewCvVehicule = em.merge(cvVehiculeListNewCvVehicule);
                    if (oldCvModelIdmodelOfCvVehiculeListNewCvVehicule != null && !oldCvModelIdmodelOfCvVehiculeListNewCvVehicule.equals(cvModel)) {
                        oldCvModelIdmodelOfCvVehiculeListNewCvVehicule.getCvVehiculeList().remove(cvVehiculeListNewCvVehicule);
                        oldCvModelIdmodelOfCvVehiculeListNewCvVehicule = em.merge(oldCvModelIdmodelOfCvVehiculeListNewCvVehicule);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = cvModel.getIdmodel();
                if (findCvModel(id) == null) {
                    throw new NonexistentEntityException("The cvModel with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            CvModel cvModel;
            try {
                cvModel = em.getReference(CvModel.class, id);
                cvModel.getIdmodel();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The cvModel with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<CvTypemodel> cvTypemodelListOrphanCheck = cvModel.getCvTypemodelList();
            for (CvTypemodel cvTypemodelListOrphanCheckCvTypemodel : cvTypemodelListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This CvModel (" + cvModel + ") cannot be destroyed since the CvTypemodel " + cvTypemodelListOrphanCheckCvTypemodel + " in its cvTypemodelList field has a non-nullable cvModelIdmodel field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            CvMarque cvMarqueIdmarque = cvModel.getCvMarqueIdmarque();
            if (cvMarqueIdmarque != null) {
                cvMarqueIdmarque.getCvModelList().remove(cvModel);
                cvMarqueIdmarque = em.merge(cvMarqueIdmarque);
            }
            List<CvVehicule> cvVehiculeList = cvModel.getCvVehiculeList();
            for (CvVehicule cvVehiculeListCvVehicule : cvVehiculeList) {
                cvVehiculeListCvVehicule.setCvModelIdmodel(null);
                cvVehiculeListCvVehicule = em.merge(cvVehiculeListCvVehicule);
            }
            em.remove(cvModel);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<CvModel> findCvModelEntities() {
        return findCvModelEntities(true, -1, -1);
    }

    public List<CvModel> findCvModelEntities(int maxResults, int firstResult) {
        return findCvModelEntities(false, maxResults, firstResult);
    }

    private List<CvModel> findCvModelEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(CvModel.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public CvModel findCvModel(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(CvModel.class, id);
        } finally {
            em.close();
        }
    }

    public int getCvModelCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<CvModel> rt = cq.from(CvModel.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    public List<CvModel> findByMarque(CvMarque cvMarque){
        EntityManager em = getEntityManager();
        try {
           String queryString = "select c from CvModel c "
                    + "where c.cvMarqueIdmarque = :x";
            Query query = em.createQuery(queryString);
            query.setParameter("x", cvMarque); 
            
            return query.getResultList();
        } finally {
            em.close();
        }
    }
    
}
